{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2005 by Free Pascal development team

    Low level file functions
    GBA does not have any drive, so no file handling is needed.
    Copyright (c) 2006 by Francesco Lombardi

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}


{****************************************************************************
                        Low level File Routines
               All these functions can set InOutRes on errors
****************************************************************************}

{ close a file from the handle value }
procedure do_close(handle : THandle);
begin
	CloseFile(handle);
end;

procedure do_erase(p : pchar);
begin
end;

procedure do_rename(p1,p2 : pchar);
begin
end;

function do_write(h: THandle; addr: pointer; len: int64) : int64;
begin
   result := WriteFile(h, addr, len);
end;

function do_read(h: THandle; addr: pointer; len: int64) : int64;
begin
   result := ReadFile(h, addr, len);
end;

function do_filepos(handle: THandle) : int64;
begin
   result := SeekFile(handle, 0,0);
end;

procedure do_seek(handle: THandle; pos: int64);
begin
   SeekFile(handle, pos,0);
end;

function do_seekend(handle: THandle):int64;
begin
   result := SeekFile(handle, 0,2);
end;

function do_filesize(handle : THandle) : int64;
begin
	result := FileSize(handle);
end;

{ truncate at a given position }
procedure do_truncate(handle: THandle; pos: int64);
begin
end;

procedure do_open(var f;p:pchar;flags:longint);
var fm: TFileFlags;
begin
{ close first if opened }
	if ((flags and $10000)=0) then
	begin
		case FileRec(f).mode of
			fminput,
			fmoutput,
			fminout:
				Do_Close(FileRec(f).Handle);
			fmclosed : ;
		else
			begin
				inoutres:=102; {not assigned}
				exit;
			end;
		end;
	end;
{ reset file Handle }
	FileRec(f).Handle:=UnusedHandle;
{ We do the conversion of filemodes here, concentrated on 1 place }
	case (flags and 3) of
		0:
			begin
				fm := [system.fmOpenRead];
				FileRec(f).mode:=fminput;
			end;
		1:
			begin
				fm := [system.fmOpenWrite];
				FileRec(f).mode:=fmoutput;
			end;
		2:
			begin
				fm := [system.fmOpenRead, system.fmOpenWrite];
				FileRec(f).mode:=fminout;
			end;
	end;
	{if (flags and $1000)=$1000 then
		oflags:=oflags or (O_CREAT or O_TRUNC)
	else if (flags and $100)=$100 then
		oflags:=oflags or (O_APPEND);}
	{ empty name is special }
	if p[0]=#0 then
	begin
		case FileRec(f).mode of
			fminput :
				FileRec(f).Handle:=StdInputHandle;
			fminout, // this is set by rewrite
			fmoutput :
				FileRec(f).Handle:=StdOutputHandle;
			fmappend :
				begin
					FileRec(f).Handle:=StdOutputHandle;
					FileRec(f).mode:=fmoutput; //fool fmappend
				end;
		end;
		exit;
	end;
	
	FileRec(f).Handle := OpenFile(p, fm);

	If Filerec(f).Handle = InvalidHandle Then
		InOutRes := 1
	else
		InOutRes := 0;
end;

function do_isdevice(handle: THandle): boolean;
begin
   do_isdevice:= (handle=StdInputHandle) or
                 (handle=StdOutputHandle) or
                 (handle=StdErrorHandle);
end;

